//
//  LivePlayViewController.m
//  LiveVideoCoreDemo
//
//  Created by 龚丁禧 on 16/4/21.
//  Copyright © 2016年 com.Alex. All rights reserved.
//
#import "LivePlayViewController.h"
#import "FFMediaPlayer+vp.h"

@interface LivePlayViewController()<FFMediaPlayerDelegate> {
    UIButton* _ExitButton;
    UIView  *_playerBackgroundView;
    FFMediaPlayer* _player;
}
@end

@implementation LivePlayViewController

- (void)UIInit
{
    CGFloat fScreenW = [UIScreen mainScreen].bounds.size.width;
    CGFloat fScreenH = [UIScreen mainScreen].bounds.size.height;
    _playerBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, fScreenW, fScreenH)];
    _playerBackgroundView.backgroundColor = [UIColor blackColor];
    [self.view addSubview: _playerBackgroundView];
    
    CGFloat fExitButtonW = 40;
    CGFloat fExitButtonH = 40;
    CGFloat fExitButtonX = fScreenW - fExitButtonW;
    CGFloat fExitButtonY = 30;
    _ExitButton = [[UIButton alloc] initWithFrame:CGRectMake(fExitButtonX, fExitButtonY, fExitButtonW, fExitButtonH)];
    _ExitButton.layer.masksToBounds = YES;
    _ExitButton.layer.cornerRadius  = 5;
    [_ExitButton setImage:[UIImage imageNamed:@"ic_live_close_normal"] forState:UIControlStateNormal];
    [_ExitButton addTarget:self action:@selector(OnExitClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_ExitButton];
}

-(void) OnExitClicked:(id)sender{
    [_player reset];
    [_player unSetupPlayer];
    _player = nil;
    
    [self dismissViewControllerAnimated: YES completion:^{
        
    }];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self UIInit];
    [self playerInit];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)playerInit {
    _player=[[FFMediaPlayer alloc] init];
    [_player setURL: self.playUrl withDelegate: self];
    [_player setupPlayerWithContainerView: _playerBackgroundView];
    _player.shouldPlayInBackground = YES;
    [_player prepareAsync];
}

/**
 * Called when the player prepared.
 *
 * @param player The shared media player instance.
 * @param arg Not use.
 */
- (void)mediaPlayer:(FFMediaPlayer *)player didPrepared:(id)arg {
    
    NSLog(@"mediaPlayer didPrepared");
    
//    [_player setVideoScalingMode:FFVideoScalingModeAspectFill animate:NO];
    [_player start];
}

/**
 * Called when the player playback completed.
 *
 * @param player The shared media player instance.
 * @param arg Not use.
 */
- (void)mediaPlayer:(FFMediaPlayer *)player playbackComplete:(id)arg {
    
}

/**
 * Called when the player have error occur.
 *
 * @param player The shared media player instance.
 * @param arg Contain the detail error information.
 */
- (void)mediaPlayer:(FFMediaPlayer *)player error:(id)arg {
    
}
@end
